﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZenHR.AttData.Collection.DataEntity;


namespace ZenHR.AttData.Collection.LinkDBHelper
{
    /// <summary>
    /// 中控考勤机访问类
    /// </summary>
    public class ZKHelper
    {
        char vbcr = Convert.ToChar(13);  //回车符
        char vblf = Convert.ToChar(10);  //换行符


        /// <summary>
        /// 获取多个考勤机内的打卡数据
        /// </summary>
        /// <param name="p_MachineList">考勤机访问信息的集合</param>
        /// <param name="p_log">错误日志</param>
        /// <returns>指定的考勤机列表中的打卡记录</returns>
        public List<AttendanceInfo> GetAttendanceData(List<MachineInfo> p_MachineList, ref string p_log)
        {
            //定义返回对象
            List<AttendanceInfo> _ReturnList = new List<AttendanceInfo>();

            foreach (MachineInfo _MachineInfo in p_MachineList)
            {
                //考机访问对象（访问类）
                zkemkeeper.CZKEM _axCZKEM1 = new zkemkeeper.CZKEM();

                try
                {
                    int idwErrorCode = 0;
                    bool bIsConnected = _axCZKEM1.Connect_Net(_MachineInfo.Ip.Trim(), _MachineInfo.Port);
                    if (bIsConnected == true)
                    {
                        _axCZKEM1.RegEvent(_MachineInfo.Machine_no, 65535);
                    }
                    else
                    {
                        _axCZKEM1.GetLastError(idwErrorCode);
                        //输出日志:XX考勤机连接失败!（IP: Port:）
                        p_log = p_log + _MachineInfo.Machine_no + "考勤机连接失败!（IP:" + _MachineInfo.Ip + "Port:" + _MachineInfo.Port + "）";
                    }

                    _axCZKEM1.EnableDevice(_MachineInfo.Machine_no, false);

                    if (_axCZKEM1.ReadGeneralLogData(_MachineInfo.Machine_no))
                    {
                        System.Data.DataTable _Table = new System.Data.DataTable();
                        _Table.Columns.Add("ID");
                        _Table.Columns.Add("Code");

                        if (_MachineInfo.Machine_type == "S")
                        {
                            string idwEnrollNumber = "";
                            //string idwName = "";
                            int idwVerifyMode = 0;
                            int idwInOutMode = 0;
                            int idwYear = 0;
                            int idwMonth = 0;
                            int idwDay = 0;
                            int idwHour = 0;
                            int idwMinute = 0;
                            int idwSecond = 0;
                            int idwWorkCode = 0;
                            //int idwReserved = 0;
                            //dynamic iGLCount = 0;

                           

                            while (_axCZKEM1.SSR_GetGeneralLogData(_MachineInfo.Machine_no, out idwEnrollNumber, out idwVerifyMode, out idwInOutMode, out idwYear, out idwMonth, out idwDay, out idwHour, out idwMinute, out idwSecond, ref idwWorkCode))
                            {
                                try
                                {
                                    ////iGLCount += 1;
                                    //int _CardNo = 0;
                                    ////try
                                    ////{
                                    ////    _CardNo = Convert.ToInt32(_Table.Select("ID='" + idwEnrollNumber + "'")[0]["Code"]);
                                    ////}
                                    ////catch (Exception)
                                    ////{
                                    //_CardNo = Convert.ToInt32(idwEnrollNumber);
                                    ////}

                                    //string _StrCardNo = _CardNo.ToString();
                                    string _StrCardNo = idwEnrollNumber;
                                    //if (_StrCardNo.Length < 10)
                                    //{
                                    //    for (int i = 0; i <= 10 - _StrCardNo.Length - 1; i++)
                                    //    {
                                    //        _StrCardNo = "0" + _StrCardNo;
                                    //    }
                                    //}
                                    string _StrMonth = idwMonth.ToString();
                                    string _StrDay = idwDay.ToString();
                                    string _StrHour = idwHour.ToString();
                                    string _StrMinute = idwMinute.ToString();
                                    string _StrSecond = idwSecond.ToString();

                                    //检测月，日，小时，分钟，秒是否需要补零
                                    if (_StrMonth.Length == 1)
                                    {
                                        _StrMonth = "0" + _StrMonth;
                                    }
                                    if (_StrDay.Length == 1)
                                    {
                                        _StrDay = "0" + _StrDay;
                                    }
                                    if (_StrHour.Length == 1)
                                    {
                                        _StrHour = "0" + _StrHour;
                                    }
                                    if (_StrMinute.Length == 1)
                                    {
                                        _StrMinute = "0" + _StrMinute;
                                    }
                                    if (_StrSecond.Length == 1)
                                    {
                                        _StrSecond = "0" + _StrSecond;
                                    }

                                    string _att_time = idwYear.ToString() + "-" + _StrMonth + "-" + _StrDay + " " + _StrHour + ":" + _StrMinute + ":" + _StrSecond;

                                    AttendanceInfo _attinfo = new AttendanceInfo();
                                    //_attinfo.MachineNo = _MachineInfo.Machine_no.ToString();
                                    _attinfo.card_number = _StrCardNo;
                                    _attinfo.inorout = idwInOutMode;
                                    _attinfo.check_time = _att_time;

                                    //将读取的考勤信息加入返回列表
                                    _ReturnList.Add(_attinfo);
                                }
                                catch (Exception ex)
                                {
                                    //记录那一条没被读取
                                    //标记执行完成以后不能删除考勤中的所有数据
                                    p_log = p_log + ex.Message;
                                }
                            }
                        }
                        else
                        {
                            int idwEnrollNumber = 0;
                            int idwVerifyMode = 0;
                            int idwInOutMode = 0;
                            int idwYear = 0;
                            int idwMonth = 0;
                            int idwDay = 0;
                            int idwHour = 0;
                            int idwMinute = 0;
                            int idwSecond = 0;
                            int idwWorkCode = 0;
                            int idwReserved = 0;
                            //dynamic iGLCount = 0;

                            while (_axCZKEM1.GetGeneralExtLogData(_MachineInfo.Machine_no, idwEnrollNumber, idwVerifyMode, idwInOutMode, idwYear, idwMonth, idwDay, idwHour, idwMinute, idwSecond,
                            idwWorkCode, idwReserved))
                            {
                                //iGLCount += 1;
                                int _CardNo = 0;
                                //try
                                //{
                                //_CardNo = Convert.ToInt32(_Table.Select("ID='" + idwEnrollNumber + "'")[0]["Code"]);
                                //}
                                //catch (Exception)
                                //{
                                _CardNo = idwEnrollNumber;
                                //}

                                string _StrCardNo = _CardNo.ToString();
                                if (_StrCardNo.Length < 10)
                                {
                                    for (int i = 0; i <= 10 - _StrCardNo.Length - 1; i++)
                                    {
                                        _StrCardNo = "0" + _StrCardNo;
                                    }
                                }
                                string _StrMonth = idwMonth.ToString();
                                string _StrDay = idwDay.ToString();
                                string _StrHour = idwHour.ToString();
                                string _StrMinute = idwMinute.ToString();
                                string _StrSecond = idwSecond.ToString();

                                //检测月，日，小时，分钟，秒是否需要补零
                                if (_StrMonth.Length == 1)
                                {
                                    _StrMonth = "0" + _StrMonth;
                                }
                                if (_StrDay.Length == 1)
                                {
                                    _StrDay = "0" + _StrDay;
                                }
                                if (_StrHour.Length == 1)
                                {
                                    _StrHour = "0" + _StrHour;
                                }
                                if (_StrMinute.Length == 1)
                                {
                                    _StrMinute = "0" + _StrMinute;
                                }
                                if (_StrSecond.Length == 1)
                                {
                                    _StrSecond = "0" + _StrSecond;
                                }
                                string _att_time = idwYear.ToString() + "-" + _StrMonth + "-" + _StrDay + " " + _StrHour + ":" + _StrMinute + ":" + _StrSecond;

                                AttendanceInfo _attinfo = new AttendanceInfo();
                                //_attinfo.MachineNo = _MachineInfo.Machine_no.ToString();
                                _attinfo.card_number = _StrCardNo;
                                _attinfo.inorout = idwInOutMode;
                                _attinfo.check_time = _att_time;

                                //将读取的考勤信息加入返回列表
                                _ReturnList.Add(_attinfo);
                            }
                        }
                    }
                    else
                    {
                        _axCZKEM1.GetLastError(idwErrorCode);
                        if (idwErrorCode != 0)
                        {
                            //p_LogStr = p_LogStr + vbcr + vblf + "读取考勤机中的数据失败.错误编码:" + idwErrorCode;
                            //p_ErrorType = "读取考勤机失败";
                        }
                        else
                        {
                            //p_LogStr = p_LogStr + vbcr + vblf + "考勤机中没有数据可被读取.错误编码:" + idwErrorCode;
                            //p_ErrorType = "考勤机无数据读取";
                        }
                    }
                    bIsConnected = false;
                }
                catch (Exception ex)
                {
                    p_log = p_log + ex.Message;
                }
            }
            return _ReturnList;
        }


        /// <summary>
        /// 清除考勤机中的考勤记录
        /// </summary>
        /// <param name="p_MachineList"></param>
        /// <param name="p_log"></param>
        /// <returns>成功与否</returns>
        public bool ClearGLog(List<MachineInfo> p_MachineList, ref string p_log)
        {
            bool _return = true;
            zkemkeeper.CZKEM _axCZKEM1 = new zkemkeeper.CZKEM();
            foreach (MachineInfo _MachineInfo in p_MachineList)
            {
                _axCZKEM1.Connect_Net(_MachineInfo.Ip.Trim(), _MachineInfo.Port);

                _axCZKEM1.EnableDevice(_MachineInfo.Machine_no, false);

                try
                {
                    int idwErrorCode = 0;
                    if (_axCZKEM1.ClearGLog(_MachineInfo.Machine_no) == true)
                    {
                        _axCZKEM1.RefreshData(_MachineInfo.Machine_no);
                    }
                    else
                    {
                        _axCZKEM1.GetLastError(idwErrorCode);
                    }
                    _axCZKEM1.EnableDevice(_MachineInfo.Machine_no, true);
                }
                catch (Exception ex)
                {
                    p_log = ex.Message;
                    _return = false;
                }
                _axCZKEM1.EnableDevice(_MachineInfo.Machine_no, true);
            }
            return _return;
        }


        /// <summary>
        /// 还原多个考勤机工作状态
        /// </summary>
        /// <param name="p_MachineList"></param>
        /// <param name="p_log"></param>
        /// <returns>成功与否</returns>
        public bool EnableDevice(List<MachineInfo> p_MachineList, ref string p_log)
        {
            bool _return = false;
            zkemkeeper.CZKEM _axCZKEM1 = new zkemkeeper.CZKEM();
            foreach (MachineInfo _MachineInfo in p_MachineList)
            {
                _axCZKEM1.Connect_Net(_MachineInfo.Ip.Trim(), _MachineInfo.Port);

                try
                {
                    _return = _axCZKEM1.EnableDevice(_MachineInfo.Machine_no, true);
                }
                catch (Exception ex)
                {
                    p_log = ex.Message;
                }
            }
            return _return;
        }


        /// <summary>
        /// 获取多个考勤机中的用户信息
        /// </summary>
        /// <param name="p_MachineList">考勤机访问信息的集合</param>
        /// <param name="data">考勤机中的用户信息</param>
        /// <param name="p_log">错误日志</param>
        /// <returns>指定的考勤机列表中的用户信息</returns>
        public List<UserInfo> GetAllUserInfo(List<MachineInfo> p_MachineList, ref string p_log)
        {
            List<UserInfo> _UserList = new List<UserInfo>();

            zkemkeeper.CZKEM _axCZKEM1 = default(zkemkeeper.CZKEM);

            foreach (MachineInfo _MachineInfo in p_MachineList)
            {
                try
                {
                    _axCZKEM1 = new zkemkeeper.CZKEM();

                    //使用网络方式连接考勤机
                    if (_axCZKEM1.Connect_Net(_MachineInfo.Ip, _MachineInfo.Port))
                    {
                        //发送读取考勤机上有用户的命令
                        //_axCZKEM1.ReadAllUserID(_MachineInfo.Machine_no);
                        _axCZKEM1.EnableDevice(_MachineInfo.Machine_no, false);

                        if (_MachineInfo.Machine_type == "S")
                        {
                            string _id = "";
                            string _Name = "";
                            string _Pwd = "";
                            int _Privilege = 0;
                            bool _ienabled = false;

                            if (_axCZKEM1.ReadAllUserID(_MachineInfo.Machine_no))
                            {
                                while (_axCZKEM1.SSR_GetAllUserInfo(_MachineInfo.Machine_no, out _id, out _Name, out _Pwd, out _Privilege, out _ienabled) == true)
                                {
                                    //string ifinger = "";
                                    //int _tmp_int = 0;

                                    UserInfo _userinfo = new UserInfo();
                                    _userinfo.Id = _id;
                                    _userinfo.Privilege = _Privilege.ToString();
                                    _userinfo.Pwd = _Pwd;

                                    string[] s = _Name.ToString().Split('\0');
                                    _userinfo.Name = s[0];
                                    //int index = _Name.IndexOf("0", StringComparison.Ordinal);
                                    //int index2 = _Name.LastIndexOf("\\");
                                    //string name = _Name.Substring(0, 3);
                                    //if (name.Substring(2, 1)=="\\")
                                    //{
                                    //    name = name.Substring(0, 2);
                                    //}
                                    //_userinfo.Name = name;
                                    //_userinfo.Name = _Name.Substring(0, 2);


                                    _userinfo.IsEdit = false.ToString();
                                    string _CardNumber = "";
                                    _axCZKEM1.GetStrCardNumber(out _CardNumber);
                                    _userinfo.CardNo = _CardNumber;

                                    //_axCZKEM1.SSR_GetUserTmpStr(_MachineInfo.Machine_no, _id, 0, out ifinger, out _tmp_int);                                        
                                    //_userinfo.Finger = ifinger;

                                    //将读取的考勤信息加入返回列表
                                    _UserList.Add(_userinfo);
                                    //_axCZKEM1.SSR_GetUserInfo(_MachineInfo.Machine_no, _id, out _Name, out _Pwd, out _Privilege, out _ienabled);
                                    //_axCZKEM1.SSR_GetUserTmpStr(_MachineInfo.Machine_no, iEnrollNumber, 0, out ifinger, out _tmp_int);
                                }
                            }
                        }
                        else
                        {
                            int iEnrollNumber = 0;
                            int iEMachineNumber = 0;
                            string iName = "";
                            string iPwd = "";
                            int iBackupNumber = 0;
                            int iPrivilege = 0;
                            //Boolean ienabled = false;
                            int dwenable = 0;

                            while (_axCZKEM1.GetAllUserID(_MachineInfo.Machine_no, ref iEnrollNumber, ref iEMachineNumber, ref iBackupNumber, ref iPrivilege, ref dwenable) == true)
                            {
                                UserInfo _userinfo = new UserInfo();
                                _userinfo.Id = iEnrollNumber.ToString();
                                _userinfo.Code = _axCZKEM1.get_CardNumber(0).ToString();
                                _userinfo.Privilege = iPrivilege.ToString();
                                _userinfo.Pwd = iPwd;
                                _userinfo.Name = iName;
                                _userinfo.IsEdit = false.ToString();
                                _UserList.Add(_userinfo);
                            }
                        }
                        _axCZKEM1.EnableDevice(_MachineInfo.Machine_no, true);
                    }
                    else
                    {
                        string tmp_log = "考勤机连接失败,IP:" + _MachineInfo.Ip;
                        p_log = String.IsNullOrEmpty(p_log) ? tmp_log : (p_log + ";" + tmp_log);
                    }
                }
                catch (Exception ex)
                {
                    p_log = ex.Message;
                }
                finally
                {
                    if ((_axCZKEM1 != null))
                    {
                        _axCZKEM1.Disconnect();
                    }
                }
            }
            return _UserList;
        }


        /// <summary>
        /// 同步员工信息
        /// </summary>
        /// <param name="p_UserList">需要同步是员工信息的集合</param>
        /// <param name="p_MachineList">考勤机访问信息的集合</param>
        /// <param name="p_log">错误日志</param>
        /// <returns>成功与否</returns>
        public bool SubSynCard(List<UserInfo> p_UserList, List<MachineInfo> p_MachineList, ref string p_log, ref int number)
        {
            bool _return = true;

            zkemkeeper.CZKEM _axCZKEM1 = default(zkemkeeper.CZKEM);

            foreach (MachineInfo _MachineInfo in p_MachineList)
            {
                try
                {
                    _axCZKEM1 = new zkemkeeper.CZKEM();

                    //var _current_MachineInfoList = new List<MachineInfo>();
                    //_current_MachineInfoList.Add(_MachineInfo);
                    ////获取对应考勤机的员工信息
                    //List<UserInfo> _M_UserInfo = GetAllUserInfo(_current_MachineInfoList, ref p_log);

                    //检测考勤机是否链接成功,如果已连接，则禁用机器
                    if (_axCZKEM1.Connect_Net(_MachineInfo.Ip, _MachineInfo.Port))
                    {
                        _axCZKEM1.EnableDevice(_MachineInfo.Machine_no, false);

                        //string[] _idstr = new string[_M_UserInfo.Count];
                        //int _index = 0;
                        //foreach (var item in _M_UserInfo)
                        //{
                        //    _idstr[_index] = item.Id;
                        //    _index = _index+1;
                        //}                
                        ////筛选一下需要插入对应考勤机的员工记录
                        //var _UserList = from aa in p_UserList where aa.MachineNo == _MachineInfo.Machine_no && !(_idstr).Contains(aa.Id) select aa;
                        //number = _UserList.Count();

                        //筛选对应考勤机的员工信息
                        var _UserList = from aa in p_UserList where aa.MachineNo == _MachineInfo.Machine_no select aa;
                        number = _UserList.Count();

                        _axCZKEM1.BeginBatchUpdate(_MachineInfo.Machine_no, 1);
                        //循环将记录插入到考勤机中
                        foreach (var item in _UserList)
                        {
                            int _Privilege;
                            int.TryParse(item.Privilege, out _Privilege);

                            _axCZKEM1.SetStrCardNumber(item.CardNo);
                            _axCZKEM1.SSR_SetUserInfo(item.MachineNo, item.Id, item.Name, item.Pwd, _Privilege, true);

                            int iFlag = 0;

                            if (item.Finger != null)
                            {
                                _axCZKEM1.SetUserTmpExStr(item.MachineNo, item.Id, 0, iFlag, item.Finger);
                            }
                            if (item.Finger1 != null)
                            {
                                _axCZKEM1.SetUserTmpExStr(item.MachineNo, item.Id, 1, iFlag, item.Finger1);
                            }
                            if (item.Finger2 != null)
                            {
                                _axCZKEM1.SetUserTmpExStr(item.MachineNo, item.Id, 2, iFlag, item.Finger2);
                            }
                            if (item.Finger3 != null)
                            {
                                _axCZKEM1.SetUserTmpExStr(item.MachineNo, item.Id, 3, iFlag, item.Finger3);
                            }
                            if (item.Finger4 != null)
                            {
                                _axCZKEM1.SetUserTmpExStr(item.MachineNo, item.Id, 4, iFlag, item.Finger4);
                            }
                            if (item.Finger5 != null)
                            {
                                _axCZKEM1.SetUserTmpExStr(item.MachineNo, item.Id, 5, iFlag, item.Finger5);
                            }
                            if (item.Finger6 != null)
                            {
                                _axCZKEM1.SetUserTmpExStr(item.MachineNo, item.Id, 6, iFlag, item.Finger6);
                            }
                            if (item.Finger7 != null)
                            {
                                _axCZKEM1.SetUserTmpExStr(item.MachineNo, item.Id, 7, iFlag, item.Finger7);
                            }
                            if (item.Finger8 != null)
                            {
                                _axCZKEM1.SetUserTmpExStr(item.MachineNo, item.Id, 8, iFlag, item.Finger8);
                            }
                            if (item.Finger9 != null)
                            {
                                _axCZKEM1.SetUserTmpExStr(item.MachineNo, item.Id, 9, iFlag, item.Finger9);
                            }

                        }
                        _axCZKEM1.BatchUpdate(_MachineInfo.Machine_no);
                        _axCZKEM1.RefreshData(_MachineInfo.Machine_no);
                        _axCZKEM1.EnableDevice(_MachineInfo.Machine_no, true);
                    }
                    else
                    {
                        p_log = "考勤机连接失败！";
                        _return = false;
                    }
                }
                catch (Exception ex)
                {
                    p_log = p_log + ex.Message;
                    _return = false;
                }
                finally
                {
                    if ((_axCZKEM1 != null))
                    {
                        _axCZKEM1.Disconnect();
                    }
                }
            }
            _axCZKEM1.Disconnect();
            return _return;
        }


        /// <summary>
        /// 设置机器\终端时间为PC的时间
        /// </summary>
        /// <param name="p_MachineList">考勤机访问信息的集合</param>
        /// <param name="p_log">错误日志</param>
        /// <returns>成功与否</returns>
        public bool SetTime(List<MachineInfo> p_MachineList, ref string p_log)
        {
            bool _return = true;
            zkemkeeper.CZKEM _axCZKEM1 = default(zkemkeeper.CZKEM);
            foreach (MachineInfo _MachineInfo in p_MachineList)
            {
                try
                {
                    _axCZKEM1 = new zkemkeeper.CZKEM();
                    if (_axCZKEM1.Connect_Net(_MachineInfo.Ip, _MachineInfo.Port))
                    {
                        _axCZKEM1.EnableDevice(_MachineInfo.Machine_no, false);
                        _axCZKEM1.SetDeviceTime(_MachineInfo.Machine_no);
                        _axCZKEM1.EnableDevice(_MachineInfo.Machine_no, true);
                    }
                }
                catch (Exception ex)
                {
                    p_log = ex.Message;
                    _return = false;
                }
            }
            return _return;
        }


        /// <summary>
        /// 清除员工信息
        /// </summary>
        /// <param name="p_MachineList">考勤机访问信息的集合</param>
        /// <param name="p_log">错误日志</param>
        /// <returns>成功与否</returns>
        public bool ClearCard(List<MachineInfo> p_MachineList, ref string p_log)
        {
            bool _return = true;
            zkemkeeper.CZKEM _axCZKEM1 = default(zkemkeeper.CZKEM);
            foreach (MachineInfo _MachineInfo in p_MachineList)
            {
                try
                {
                    _axCZKEM1 = new zkemkeeper.CZKEM();
                    if (_axCZKEM1.Connect_Net(_MachineInfo.Ip, _MachineInfo.Port))
                    {
                        _axCZKEM1.EnableDevice(_MachineInfo.Machine_no, false);

                        var _current_MachineInfoList = new List<MachineInfo>();
                        _current_MachineInfoList.Add(_MachineInfo);
                        //获取对应考勤机的员工信息
                        List<UserInfo> _M_UserInfo = GetAllUserInfo(_current_MachineInfoList, ref p_log);

                        //删除考勤机所有员工信息（0代表删除第一个指纹，12代表删除所有信息）
                        foreach (var item in _M_UserInfo)
                        {
                            //int _dwENumber;
                            //int.TryParse(item.Id, out _dwENumber);
                            _axCZKEM1.SSR_DeleteEnrollData(Convert.ToInt32(item.MachineNo), item.Id, 12);
                        }
                        _axCZKEM1.RefreshData(_MachineInfo.Machine_no);
                        _axCZKEM1.EnableDevice(_MachineInfo.Machine_no, true);
                    }
                }
                catch (Exception ex)
                {
                    p_log = ex.Message;
                    _return = false;
                }
            }
            return _return;
        }


        /// <summary>
        /// 更新员工信息
        /// </summary>
        /// <param name="p_MachineList"></param>
        /// <param name="p_log"></param>
        /// <returns>成功与否</returns>
        public bool ReviseUser(List<UserInfo> p_UserList, List<MachineInfo> p_MachineList, ref string p_log)
        {
            bool _return = true;
            zkemkeeper.CZKEM _axCZKEM1 = default(zkemkeeper.CZKEM);
            foreach (MachineInfo _MachineInfo in p_MachineList)
            {
                try
                {
                    _axCZKEM1 = new zkemkeeper.CZKEM();
                    //检测考勤机是否链接成功,如果已连接，则关闭
                    if (_axCZKEM1.Connect_Net(_MachineInfo.Ip, _MachineInfo.Port))
                    {
                        _axCZKEM1.EnableDevice(_MachineInfo.Machine_no, false);

                        ////筛选一下需要更新的员工记录
                        //var _UserList = from aa in p_UserList where aa.MachineNo == _MachineInfo.Machine_no select aa;

                        //循环将记录插入到考勤机中
                        foreach (var item in p_UserList)
                        {
                            int _Privilege;
                            int.TryParse(item.Privilege, out _Privilege);

                            _axCZKEM1.SSR_SetUserInfo(item.MachineNo, item.Id, item.Name, item.Pwd, _Privilege, true);

                            _axCZKEM1.RefreshData(item.MachineNo);

                            int iFlag = 0;

                            if (item.Finger != null)
                            {
                                _axCZKEM1.SetUserTmpExStr(item.MachineNo, item.Id, 0, iFlag, item.Finger);
                            }
                            if (item.Finger1 != null)
                            {
                                _axCZKEM1.SetUserTmpExStr(item.MachineNo, item.Id, 1, iFlag, item.Finger1);
                            }
                            if (item.Finger2 != null)
                            {
                                _axCZKEM1.SetUserTmpExStr(item.MachineNo, item.Id, 2, iFlag, item.Finger2);
                            }
                            if (item.Finger3 != null)
                            {
                                _axCZKEM1.SetUserTmpExStr(item.MachineNo, item.Id, 3, iFlag, item.Finger3);
                            }
                            if (item.Finger4 != null)
                            {
                                _axCZKEM1.SetUserTmpExStr(item.MachineNo, item.Id, 4, iFlag, item.Finger4);
                            }
                            if (item.Finger5 != null)
                            {
                                _axCZKEM1.SetUserTmpExStr(item.MachineNo, item.Id, 5, iFlag, item.Finger5);
                            }
                            if (item.Finger6 != null)
                            {
                                _axCZKEM1.SetUserTmpExStr(item.MachineNo, item.Id, 6, iFlag, item.Finger6);
                            }
                            if (item.Finger7 != null)
                            {
                                _axCZKEM1.SetUserTmpExStr(item.MachineNo, item.Id, 7, iFlag, item.Finger7);
                            }
                            if (item.Finger8 != null)
                            {
                                _axCZKEM1.SetUserTmpExStr(item.MachineNo, item.Id, 8, iFlag, item.Finger8);
                            }
                            if (item.Finger9 != null)
                            {
                                _axCZKEM1.SetUserTmpExStr(item.MachineNo, item.Id, 9, iFlag, item.Finger9);
                            }
                            _axCZKEM1.RefreshData(item.MachineNo);
                        }
                        _axCZKEM1.EnableDevice(_MachineInfo.Machine_no, true);
                    }
                    else
                    {
                        p_log = "考勤机连接失败！";
                        _return = false;
                    }
                }
                catch (Exception ex)
                {
                    p_log = p_log + ex.Message;
                    _return = false;
                }
            }
            return _return;
        }


        /// <summary>
        /// 删除某员工
        /// </summary>
        /// <param name="p_MachineList"></param>
        /// <param name="p_log"></param>
        /// <returns>成功与否</returns>
        public bool DelUser(List<UserInfo> p_UserList, List<MachineInfo> p_MachineList, ref string p_log)
        {
            bool _return = true;
            zkemkeeper.CZKEM _axCZKEM1 = default(zkemkeeper.CZKEM);
            foreach (MachineInfo _MachineInfo in p_MachineList)
            {
                try
                {
                    _axCZKEM1 = new zkemkeeper.CZKEM();
                    if (_axCZKEM1.Connect_Net(_MachineInfo.Ip, _MachineInfo.Port))
                    {
                        _axCZKEM1.EnableDevice(_MachineInfo.Machine_no, false);

                        ////筛选一下需要删除的员工
                        //var _UserList = from aa in p_UserList where aa.MachineNo == _MachineInfo.Machine_no select aa;

                        //删除考勤机员工信息（0代表删除第一个指纹，12代表删除所有信息）
                        foreach (var item in p_UserList)
                        {
                            //int _dwENumber;
                            //int.TryParse(item.Id, out _dwENumber);
                            //_axCZKEM1.SSR_DeleteEnrollData(Convert.ToInt32(item.MachineNo), item.Id, 12);
                            _axCZKEM1.SSR_DeleteEnrollDataExt(Convert.ToInt32(item.MachineNo), item.Id, 12);
                        }
                        _axCZKEM1.RefreshData(_MachineInfo.Machine_no);
                        _axCZKEM1.EnableDevice(_MachineInfo.Machine_no, true);
                    }
                }
                catch (Exception ex)
                {
                    p_log = ex.Message;
                    _return = false;
                }
            }
            return _return;
        }

        /// <summary>
        /// 删除所有员工
        /// </summary>
        /// <param name="p_UserList"></param>
        /// <param name="p_MachineList"></param>
        /// <param name="p_log"></param>
        /// <returns></returns>
        public bool DelAllUser(List<MachineInfo> p_MachineList, ref string p_log)
        {
            bool _return = true;
            try
            {
                zkemkeeper.CZKEM _axCZKEM1 = default(zkemkeeper.CZKEM);
                foreach (MachineInfo _MachineInfo in p_MachineList)
                {
                    _axCZKEM1 = new zkemkeeper.CZKEM();
                    if (_axCZKEM1.Connect_Net(_MachineInfo.Ip, _MachineInfo.Port))
                    {
                        _axCZKEM1.EnableDevice(_MachineInfo.Machine_no, false);
                        //_axCZKEM1.ClearData(_MachineInfo.Machine_no,5);
                        _axCZKEM1.ClearKeeperData(_MachineInfo.Machine_no);
                        //_axCZKEM1.RefreshData(_MachineInfo.Machine_no);
                        //_axCZKEM1.EnableDevice(_MachineInfo.Machine_no, true);                    
                    }
                }
            }
            catch (Exception ex)
            {
                p_log = ex.Message;
                _return = false;
            }
            return _return;
        }
    }
}
